package com.dmai.capacity.platform.oauth.common.service.impl;

import cn.hutool.core.date.DateUtil;
import com.dmai.capacity.platform.common.dto.GatewayLogGroupResult;
import com.dmai.capacity.platform.common.model.*;
import com.dmai.capacity.platform.common.utils.DateUtils;
import com.dmai.capacity.platform.oauth.common.mapper.GatewayLogDao;
import com.dmai.capacity.platform.oauth.common.mapper.OauthUserReportDayDao;
import com.dmai.capacity.platform.oauth.common.mapper.OauthUserReportTotalDao;
import com.dmai.capacity.platform.oauth.common.service.ICapabilityVolumeChargeRuleService;
import com.dmai.capacity.platform.oauth.common.service.IOauthUserReportService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class OauthUserReportServiceImpl implements IOauthUserReportService {

    @Autowired
    private OauthUserReportTotalDao totalReportDao;

    @Autowired
    private OauthUserReportDayDao dayReportDao;

    @Autowired
    private ICapabilityVolumeChargeRuleService volumeChargeRuleService;

    @Autowired
    private GatewayLogDao logDao;

    @Override
    public int reCalculate() {
        List<GatewayLogGroupResult> groupResults = logDao.groupByUserCapability(DateUtil.formatDate(DateUtil.yesterday()) + " 00:00:00", DateUtil.formatDate(DateUtil.yesterday()) + " 23:59:59");
        if (CollectionUtils.isEmpty(groupResults)) {
            return 0;
        }
        //更新日报表
        updateDayReport(groupResults);

//        //更新总报表
//        updateTotalReport(groupResults);

        return 1;
    }


    /**
     * 更新日报表
     */
    private int updateDayReport(List<GatewayLogGroupResult> groupResults) {
        int _result = 0;
        for (GatewayLogGroupResult item : groupResults) {
            OauthUserReportDay reportDay = dayReportDao.find(item.getUserid(), item.getClientId(), item.getCapabilityId(), DateUtil.formatDate(DateUtil.yesterday()));
            if (reportDay == null) {
                reportDay = new OauthUserReportDay();
                reportDay.setUserid(item.getUserid());
                reportDay.setClientId(item.getClientId());
                reportDay.setCapabilityId(item.getCapabilityId());
                reportDay.setDay(DateUtil.formatDate(DateUtil.yesterday()));
                reportDay.setVolumeChargeCount(item.getCount());
                reportDay.setUpdateTime(new Date());

                _result += dayReportDao.save(reportDay);
            } else {
                reportDay.setVolumeChargeCount(item.getCount());
                _result += dayReportDao.update(reportDay);
            }
        }
        return _result;
    }

    /**
     * 更新总报表
     */
    private int updateTotalReport(List<GatewayLogGroupResult> groupResults) {
        int _result = 0;
        for (GatewayLogGroupResult item : groupResults) {
            OauthUserReportTotal reportTotal = totalReportDao.find(item.getUserid(), item.getClientId(), item.getCapabilityId());
            if (reportTotal == null) {
                reportTotal = new OauthUserReportTotal();
                reportTotal.setUserid(item.getUserid());
                reportTotal.setClientId(item.getClientId());
                reportTotal.setCapabilityId(item.getCapabilityId());
                reportTotal.setVolumeChargeCount(item.getCount());
                reportTotal.setAmount(volumeChargeRuleService.getAmount(item.getCapabilityId(), item.getCount()));
                reportTotal.setUpdateTime(new Date());

                _result += totalReportDao.save(reportTotal);
            } else {
                reportTotal.setVolumeChargeCount(item.getCount());
                reportTotal.setAmount(volumeChargeRuleService.getAmount(item.getCapabilityId(), item.getCount()));
                _result += totalReportDao.update(reportTotal);
            }
        }
        return _result;
    }
}
